import axios from "axios";
import qs from 'qs'

const net = getNet();

function getNet() {
    return axios.create({
        baseURL: "/api",
        timeout: 20000,
        withCredentials: true,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        ,
        transformRequest: [function (data) {
            // let ret = ''
            // for (let it in data) {
            //     ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
            // }
            // return ret
            return qs.stringify(data);
        }]
    })
}

export * from './LogAnalyzeService'
export * from './SystemMonitorService'
export * from './UserGroupService'
export * from './DataVisualService'


export const request = {
    // 网络请求
    get: function (param) {
        net.get(param.url)
            .then(this.success)
            .then(param.success).catch(param.error || this.error)
    },
    post: function (param) {
        net.post(param.url, param.data)
            .then(this.success)
            .then(param.success).catch(param.error || this.error)
    },
    delete: function (param) {
        net.delete(param.url, param.data)
            .then(this.success)
            .then(param.success).catch(param.error || this.error)
    },
    put: function (param) {
        net.put(param.url, param.data)
            .then(this.success)
            .then(param.success).catch(param.error || this.error)
    },
    success: function (res) {
        return new Promise((resove, reject) => {
            const {
                data: {
                    status,
                    data,
                    msg
                }
            } = res;
            // 请求成功
            if (0 === status) {
                resove({
                    data,
                    msg
                });
            }
            // 请求数据错误
            else {
                reject(msg)
            }
        })
    },
    error: function (err) {
        alert("接口调用出错:" + err)
    }
};